package com.shiliao.framework.util;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shiliao.framework.R;
import com.shiliao.framework.YimeiFrameworkAppliction;

import static android.os.Looper.getMainLooper;

public class ToastUtils {
    private static Context context = YimeiFrameworkAppliction.getInstance();// App生命周期中唯一Context，BaseApplication继承Application
    private static LayoutInflater inflater = LayoutInflater.from(context);
    private static View myToastView = inflater.inflate(
            R.layout.layout_for_toast, null);
    private static TextView msgView = (TextView) myToastView
            .findViewById(R.id.tv_message);

    private static final int TYPE_CODE_SUCCESS = 0x01;
    private static final int TYPE_CODE_ERROR = 0x02;
    private static final int DEFAULT_TIME_DELAY = 50;// 单位：毫秒

    private static Toast toast;// 系统提示类
    private static Handler handler;

    public static void toast(String msg) {
        if (show(TYPE_CODE_SUCCESS, msg)) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, 0, 30);
            msgView.setLayoutParams(params);
        }
    }

    public static void toastError(String msg) {
        show(TYPE_CODE_ERROR, msg);
    }

    private static boolean show(final int typeCode, final String msg) {
        if (context == null || SystemUtils.isBackground(context) == true// 如果APP回到后台，则不显示
                || StringUtils.isEmpty(msg)) {
            return false;
        }

        try {
            if (toast == null) {// 防止重复提示：不为Null，即全局使用同一个Toast实例
                if (Looper.myLooper() == null) {
                    Looper.prepare();
                }
                toast = new Toast(context);
            }

            if (handler == null) {
                handler = new Handler(getMainLooper());
            }
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    msgView.setText(msg);
                    toast.setView(myToastView);
                    // toast.setDuration(Toast.LENGTH_SHORT);
                    AlphaAnimation animation = new AlphaAnimation(1, 0);
                    animation.setDuration(2000);
                    animation.setFillBefore(true);
                    msgView.startAnimation(animation);
                    toast.show();
                    if (Looper.myLooper() != null) {
                        Looper.loop();
                    }
                }
            }, DEFAULT_TIME_DELAY);
            return true;
        } catch (Exception e) {
            LogUtil.e(e.getLocalizedMessage());
        }
        return false;
    }

}
